extends Control

# UI组件引用
@onready var result_label: Label = $GameOverPanel/VBoxContainer/ResultLabel
@onready var message_label: Label = $GameOverPanel/VBoxContainer/MessageLabel
@onready var play_again_button: Button = $GameOverPanel/VBoxContainer/ButtonContainer/PlayAgainButton
@onready var main_menu_button: Button = $GameOverPanel/VBoxContainer/ButtonContainer/MainMenuButton

# 信号
signal play_again_requested
signal main_menu_requested

func _ready():
	# 初始时隐藏
	visible = false
	
	# 确保按钮可以接收输入
	if play_again_button:
		play_again_button.disabled = false
		print("GameOverScreen: 重新开始按钮已启用")
	if main_menu_button:
		main_menu_button.disabled = false
		print("GameOverScreen: 主菜单按钮已启用")
	
	# 确保GameOverScreen可以接收输入
	mouse_filter = Control.MOUSE_FILTER_STOP
	print("GameOverScreen: _ready函数执行完成")

func show_victory():
	"""显示胜利界面"""
	result_label.text = "胜利！"
	result_label.modulate = Color.GOLD
	message_label.text = "恭喜你获得了胜利！\n你成功击败了AI对手！"
	message_label.modulate = Color.WHITE
	_show_screen()

func show_defeat():
	"""显示失败界面"""
	result_label.text = "失败"
	result_label.modulate = Color.RED
	message_label.text = "很遗憾，你失败了...\n不要气馁，再试一次吧！"
	message_label.modulate = Color.WHITE
	_show_screen()

func _show_screen():
	"""显示游戏结束界面"""
	print("GameOverScreen: 开始显示游戏结束界面")
	visible = true
	
	# 确保按钮可以点击
	if play_again_button:
		play_again_button.disabled = false
		print("GameOverScreen: 重新开始按钮状态 - disabled:", play_again_button.disabled)
	if main_menu_button:
		main_menu_button.disabled = false
		print("GameOverScreen: 主菜单按钮状态 - disabled:", main_menu_button.disabled)
	
	# 添加淡入动画
	modulate = Color.TRANSPARENT
	var tween = create_tween()
	tween.tween_property(self, "modulate", Color.WHITE, 0.5)
	print("GameOverScreen: 界面已设置为可见，开始淡入动画")

func hide_screen():
	"""隐藏游戏结束界面"""
	visible = false

func _on_play_again_button_pressed():
	"""重新开始按钮点击"""
	print("GameOverScreen: 重新开始按钮被点击")
	play_again_requested.emit()
	print("GameOverScreen: 已发送 play_again_requested 信号")

func _on_main_menu_button_pressed():
	"""主菜单按钮点击"""
	print("GameOverScreen: 主菜单按钮被点击")
	main_menu_requested.emit()
	print("GameOverScreen: 已发送 main_menu_requested 信号")